import { Toast } from "vant";
import { Image as VanImage } from "vant";
import cTitle from "components/title";
import { scrollMixin } from "../../../utils/mixin";
export default {
  components: {
    cTitle,
    VanImage
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      storeId: 0,
      isLoadMore: true,
      page: 1,
      totalPage: 0,
      incomeList: [],
      userInfo: {},
      finishMoney: 0,
      waitMoney: 0,
      sumMoney: 0,
      incomeTabs: [
        {
          name: "全部",
          status: 0
        },
        {
          name: "已结算",
          status: 2
        },
        {
          name: "未结算",
          status: 1
        }
      ],
      incomeStatus: 0
    };
  },
  activated() {
    if ((this.$route.query.storeId ?? "") !== "") {
      this.storeId = Number(this.$route.query.storeId);
    }
    this.incomeLists();
  },
  methods: {
    onIncomeChange(event) {
      this.incomeStatus = event;
      this.page = 1;
      this.incomeLists();
    },
    incomeLists() {
      let json = {
        cstore_id: this.storeId,
        status: this.incomeStatus
      };
      $http.post("plugin.coupon-store.frontend.store-manage.moneyList", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            let list = res.data;
            this.isLoadMore = true;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.totalPage = 0;
            }
            this.incomeList = list;
            this.userInfo = res.member;
            this.finishMoney = res.finish_money;
            this.waitMoney = res.wait_money;
            this.sumMoney = res.sum_money;
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.totalPage) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          cstore_id: this.storeId,
          status: this.incomeStatus,
          page: this.page
        };
        $http.post("plugin.coupon-store.frontend.store-manage.moneyList", json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.isLoadMore = true;
              this.incomeList = [...this.incomeList, ...list];
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    },
    handleScroll() {
      //滚动事件触发
      if (this.getScrollTop() + this.getClientHeight() + 105 > this.getScrollHeight()) {
        //此处发起请求
        if (this.isLoadMore) {
          this.getMoreData();
        } else {
          console.log("没有更多数据");
        }
      }
    }
  }
};
